package com.media.themovieworld.common.fragment;

import io.vov.vitamio.utils.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;

import com.media.themovieworld.R;
import com.media.themovieworld.activity.MainActivity;
import com.media.themovieworld.common.HeaderBarController;
import com.media.themovieworld.common.HeaderBarController.OnHeaderBarClickListener;
import com.media.themovieworld.widget.popupwindow.MovieSearchPopup;

public abstract class MovieWorldScreen extends BaseFragmentScreen implements OnHeaderBarClickListener{

	protected HeaderBarController mHeaderBarController;
	protected View layoutRetry;
	protected boolean mInitRetryView = false;
	
	@Override
	protected View onCreateHeaderView(LayoutInflater inflater,
			ViewGroup container) {
		mHeaderBarController = new HeaderBarController(this);
		View headerView = mHeaderBarController.onCreateHeaderView(inflater, container);
		mHeaderBarController.setOnHeaderBarClickListener(this);
		return headerView;
	}
	
	public MainActivity getMainActivity(){
		return ((MainActivity)getActivity());
	}
	
	public String getTitle(){
		return getResources().getString(R.string.app_name);
	}
	
	@Override
	public void onSearchButtonClick(View v) {
		new MovieSearchPopup(getActivity()).show(v);
	}
	
	public void addRetryView(){
		if(getContentView() != null && layoutRetry!=null){
			if(this.mInitRetryView == false){
				initRetryLayout();
				getContentView().addView(layoutRetry, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
				mInitRetryView = true;
			} else {
				layoutRetry.setVisibility(View.VISIBLE);
			}
		}
	}
	
	public void removeRetryView(){
		if(getContentView()!= null && layoutRetry !=null && mInitRetryView ){
			layoutRetry.setVisibility(View.GONE);
			getContentView().removeView(layoutRetry);
			mInitRetryView = false;
		}else{
			Log.d("NULL");
		}
	}
	
	private void initRetryLayout(){
		if(layoutRetry!=null || getActivity() == null) return;
		layoutRetry = LayoutInflater.from(getActivity()).inflate(R.layout.layout_retry, null);
		Button btnTryAgain = (Button) layoutRetry.findViewById(R.id.tmw__list_efilm_activity_btnRetry);
		btnTryAgain.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onRetry();
			}
		});
		
	}
	
	protected void onRetry(){
		
	}
}
